<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.ResultSet, java.util.regex.*"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<jsp:useBean id="bt" class="store.book.javabean.Books_Table"></jsp:useBean>
<%
	String user_email = (String) session.getAttribute("user_email");
	if (user_email == null) {//如果用户没有登录就直接访问则跳转到index.jsp
		response.sendRedirect("index.jsp");
	} else {
		String bookid = request.getParameter("bookid");
		String booknum = request.getParameter("booknum");
		//int booknumInt = 0;//选购某本书的数量
		//double bookprice = 0;
		double price = 0;
		int num = 0;
		double[] totalArr = null;
		double total = 0;
		if (bookid == null || booknum == null) {//如果用户登录但是非法访问此页则跳转到index.jsp
			response.sendRedirect("index.jsp");
		} else {
			try {
			session.setAttribute("bookid", bookid);
			session.setAttribute("booknum", booknum);
			//long bookidNum = Long.parseLong(bookid);
			//booknumInt = Integer.parseInt(booknum);
			//bt.setBook_id(bookidNum);
			//ResultSet rs = bt.getBooksInfoById(bt);
%>

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>订单结算 - BookStore</title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
      	<!-- 引入 Bootstrap -->
      	<link href="css/bootstrap.min.css" rel="stylesheet">
		<link href="css/bootstrap-theme.min.css" rel="stylesheet">
		<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
      	<!-- HTML5 Shim 和 Respond.js 用于让 IE8 支持 HTML5元素和媒体查询 -->
      	<!-- 注意： 如果通过 file://  引入 Respond.js 文件，则该文件无法起效果 -->
      	<!--[if lt IE 9]>
         	<script src="js/html5shiv.min.js"></script>
        	<script src="js/respond.min.js"></script>
     	 <![endif]-->
	</head>
	
	<body>
	
	<%
		String tradeErr = (String) session.getAttribute("tradeErr");
		if (tradeErr != null) {
	%>
	<div class="alert alert-danger alert-dismissable">
		<button type="button" class="close" data-dismiss="alert"
			aria-hidden="true">&times;</button>
		<p id="error"><%=tradeErr%></p>
	</div>
	<%
			session.removeAttribute("tradeErr");
		}
	%>
	
		<form action="servlet/VerifyTrade" method="post">
		<div class="panel panel-success">
			<div class="panel-heading">
				<span class="glyphicon glyphicon-credit-card"></span>&nbsp;<span>结算</span>
			</div>
			<div class="panel-body">
				<ul class="list-group">
					<li class="list-group-item">
						<label for="buyMan">收货人：</label>
						<input type="text" class="form-control" name="buyMan" id="buyMan" />
					</li>
					<li class="list-group-item">
						<label for="address">详细地址：</label>
						<input type="text" class="form-control" name="address" id="address" />
					</li>
					<li class="list-group-item">
						<label for="phoneNum">手机号：</label>
						<input type="text" class="form-control" name="phoneNum" id="phoneNum" />
					</li>
					<li class="list-group-item">
						<label for="email">邮箱：</label>
						<input type="email" class="form-control" name="email" id="email" />
					</li>
					<li class="list-group-item">
						<label>送货清单：</label>
						<ul class="list-inline">
						<%
							String[] bookidArr = bookid.split("\\,");
							String[] booknumArr = booknum.split("\\,");
							for (int i = 0; i < bookidArr.length; i++) {
								bt.setBook_id(Long.parseLong(bookidArr[i]));
								ResultSet rs = bt.getBooksInfoById(bt);
							while (rs.next()) {
						%>
						
							<li>
								<ul class="list-unstyled well">
									<li><span class="text-muted">书名：</span><strong><%=rs.getString("book_name")%></strong></li>
									<li><span class="text-muted">作者：</span><span class="text-info"><%=rs.getString("book_author")%></span></li>
									<li><span class="text-muted">单价：</span><span class="text-danger">¥</span><span class="text-danger"><fmt:formatNumber value='<%=rs.getDouble("book_price")%>' pattern="0.00"></fmt:formatNumber></span></li>
									<li><span class="text-muted">数量：</span><span class="text-success"><%=booknumArr[i]%></span><span class="text-success">&nbsp;本</span></li>
								</ul>
							</li>
						
						<%
							price = rs.getDouble("book_price");//书的单价
							num = Integer.parseInt(booknumArr[i]); //选择书的数量
							totalArr = new double[bookidArr.length];
							totalArr[i] = price * num;
							total = total + totalArr[i];
						}
						
						}
						
						} catch (Exception e) {}%>
						</ul>
					</li>
					<li class="list-group-item">
						<label>应付总额：</label>
						<div><span class="text-success">￥</span><span class="text-success"><fmt:formatNumber value='<%=total%>' pattern="0.00"></fmt:formatNumber></span></div>
					</li>
				</ul>
			</div>
			<div class="panel-footer" style="height: 41px;">
				<button type="submit" class="btn btn-success pull-right" style="margin-top: -7px;">提交订单</button>
			</div>
		</div>
		</form>

		<!--支付成功窗口-->
		<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
		<div class="modal-dialog">
			<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					&times;
				</button>
			</div>
			<div class="modal-body">
				<span class="glyphicon glyphicon-ok-sign pull-left" style="font-size: 100px; color: palegreen;"></span>
				<h1><strong>支付成功!</strong></h1>
				<p class="text-muted">共消费了<fmt:formatNumber value='<%=total%>' pattern="0.00"></fmt:formatNumber>元<a href="index.jsp">&nbsp;&nbsp;购买更多...</a></p>
			</div>
		</div><!-- /.modal-content -->
		</div><!-- /.modal -->
		</div>

    	<!-- jQuery (Bootstrap 的 JavaScript 插件需要引入 jQuery) -->
    	<script src="js/jquery-3.1.1.min.js"></script>
    	<!-- 包括所有已编译的插件 -->
    	<script src="js/bootstrap.min.js"></script>
    	
    	<%
    		String tradeSuccess = (String) session.getAttribute("tradeSuccess");
    		if (tradeSuccess != null) {
    			if ("success".equals(tradeSuccess)) {
    	%>
    	<script type="text/javascript">
    		//页面加载后自动弹出模态框
    		$(function(){
        		$('#myModal').modal({
        			show:true,
        			backdrop:"static",
        		})
    		});
    	</script>
    	<%
    				session.removeAttribute("tradeSuccess");
    			}
    		}
    	%>
	</body>
</html>
<%}}%>